package main.com.cyz.LinkList;

import main.com.cyz.LinkList.Bean.ListNode;

/**
 * @author fox
 * @version 1.0
 * @description
 * 力扣82
 * 给定一个已排序的链表的头 head ， 删除原始链表中所有重复数字的节点，只留下不同的数字 。返回 已排序的链表 。
 * 示例 1：
 *
 * 输入：head = [1,2,3,3,4,4,5]
 * 输出：[1,2,5]
 * 示例 2：
 *
 * 输入：head = [1,1,1,2,3]
 * 输出：[2,3]
 *
 *
 * 提示：
 *
 * 链表中节点数目在范围 [0, 300] 内
 * -100 <= Node.val <= 100
 * 题目数据保证链表已经按升序 排列
 * @date 2024/5/17 21:24
 */
public class LeetCode82 {

    public ListNode point(ListNode head){
        if (head == null || head.next == null){
            return head;
        }
        ListNode sentry = new ListNode(-1,head);
        ListNode a = sentry;
        ListNode b = a.next;
        ListNode c = b.next;
        while (b != null && c != null){
            if (b.val == c.val){
                c = c.next;
                while (c != null && c.val == b.val){
                    c = c.next;
                }
                a.next = c;
                b = a.next;
                c = b.next;
            }else{
                a = a.next;
                b = a.next;
                c = b.next;
            }
        }
        return sentry.next;
    }

    public ListNode recursion(ListNode head){
        if (head == null || head.next == null){
            return head;
        }
        if (head.val == head.next.val){
            ListNode node = head.next.next;
            while (node != null && (node.val == head.val)){
                node = node.next;
            }
            return recursion(node);
        }else{
            head.next = recursion(head.next);
            return head;
        }
    }

}
